package com.yinshan.avchatdemo;


import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import com.yinshan.avchatdemo.activity.AVChatCallActivity;
import com.yinshan.avchatdemo.activity.LoginActivity;
import com.yinshan.avchatdemo.utils.SPUtils;
import com.yinshan.avchatdemo.utils.string.MD5;
import com.netease.nimlib.sdk.NIMClient;
import com.netease.nimlib.sdk.RequestCallback;
import com.netease.nimlib.sdk.auth.AuthService;
import com.netease.nimlib.sdk.auth.LoginInfo;
import com.yinshan.avchatkit.AVChatKit;

import java.lang.ref.WeakReference;

public class MainActivity extends AppCompatActivity {
    private Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context = new WeakReference<Context>(this).get();
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus) {
            LoginInfo loginInfo = getLoginInfo();
            if (null != loginInfo) {
                doLogin(loginInfo);
            } else {
                Intent intent = new Intent(MainActivity.this, LoginActivity.class);
                startActivity(intent);
            }
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    finish();
                }
            }, 500);
        }
    }


    /**
     * 执行登陆
     *
     * @param loginInfo
     */
    private void doLogin(LoginInfo loginInfo) {
        RequestCallback<LoginInfo> callback = new RequestCallback<LoginInfo>() {
            @Override
            public void onSuccess(LoginInfo loginInfo) {
                Toast.makeText(context, "登陆成功！", Toast.LENGTH_SHORT).show();
                AVChatKit.setAccount(loginInfo.getAccount());
                //跳转到拨打界面
                Intent intent = new Intent(context, AVChatCallActivity.class);
                startActivity(intent);
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        finish();
                    }
                }, 500);
            }

            @Override
            public void onFailed(int i) {
                Toast.makeText(context, "登陆失败，错误码：" + i, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onException(Throwable throwable) {
                Toast.makeText(context, "登陆异常，" + throwable.toString(), Toast.LENGTH_SHORT).show();
            }
        };
        NIMClient.getService(AuthService.class).login(loginInfo).setCallback(callback);
    }


    private LoginInfo getLoginInfo() {
        String account = (String) SPUtils.get(this, "YX_ACCOUNT", "");
        String token = (String) SPUtils.get(this, "YX_TOKEN", "");
        Log.d("Mainiactivity",account+"   "+token);
        if (!TextUtils.isEmpty(account) && !TextUtils.isEmpty(token)) {
            return new LoginInfo(account, token);
        } else {
            return null;
        }
    }

}
